package com.fanxl.securityfive.config;

import com.fanxl.securityfive.bean.MyUser;
import com.fanxl.securityfive.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author fanxl12
 * @description 实现官方提供的UserDetailsService接口即可
 * @date 2019/11/25 16:26
 */
@Service
@Slf4j
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MyUser user = userService.getUserByUsername(username);
        if (user == null) {
            throw new  UsernameNotFoundException("该用户不存在");
        }
        log.info("用户名："+username+" 角色："+user.getAuthorities().toString());
        return user;
    }
}
